package com.feihao.zhou.dao;

import com.feihao.zhou.domain.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class StudentMapper implements StudentDaoInterface {

    @Override
    public List<Student> findAllStudent() {
        List<Student> studentList = null;
        //1.加载配置：
        try {
            InputStream is = Resources.getResourceAsStream("mybatisConfig.xml");

            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

            SqlSession sqlSession = sqlSessionFactory.openSession();

            studentList = sqlSession.selectList("studentMapper.selectAll");

            //施放资源：
            is.close();
            sqlSession.close();
            return studentList;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public void addStudent(Student student) {
        //1.加载核心配置文件
        try {
            InputStream is = Resources.getResourceAsStream("mybatisConfig.xml");
            //2.获取SqlSession工厂对象：
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

            //3.获取sqlSession对象：
            SqlSession sqlSession = sqlSessionFactory.openSession();

            //执行sql:
            sqlSession.insert("studentMapper.insert", student);

            //提交事务：！！！！
            sqlSession.commit();
            sqlSession.close();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteStudent(String sid) {
        //1.加载核心配置文件：
        try {
            InputStream is = Resources.getResourceAsStream("mybatisConfig.xml");

            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

            SqlSession sqlSession = sqlSessionFactory.openSession();

            sqlSession.delete("studentMapper.delete", sid);

            //提交事务：
            sqlSession.commit();

            //光流：
            is.close();
            sqlSession.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateStudent(Student student) {
        //1.加载peizhiwenj：
        try {
            InputStream is = Resources.getResourceAsStream("mybatisConfig.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            int update = sqlSession.update("studentMapper.update", student);
            //提交事务：
            sqlSession.commit();

            //关闭流：
            is.close();
            sqlSession.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Student findStudentById(String sid) {
        //1.加载配置文件：
        try {
            InputStream is = Resources.getResourceAsStream("mybatisConfig.xml");

            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

            SqlSession sqlSession = sqlSessionFactory.openSession();

            Student student = sqlSession.selectOne("studentMapper.selectOne", sid);

            is.close();
            sqlSession.close();

            return student;


        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
